package com.ele.client.activity.editor.widget;

import com.ele.model.Word;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.event.CloseEvent;
import com.github.gwtbootstrap.client.ui.event.CloseHandler;
import com.github.gwtbootstrap.client.ui.event.ClosedEvent;
import com.github.gwtbootstrap.client.ui.event.ClosedHandler;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.List;

public class WordEditorWidget extends Composite implements IsEditorWidget<Word> {

    interface DefaultUiBinder extends UiBinder<Widget, WordEditorWidget> {}
    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    private Long wordId;

    @UiField
    TextBox textTextBox;
    @UiField
    TextBox translationTextBox;
    @UiField
    Button deleteButton;

    private List<FocusWidget> controls = new ArrayList<FocusWidget>();

    public WordEditorWidget(Word word) {
        HorizontalPanel widget = (HorizontalPanel) uiBinder.createAndBindUi(this);
        initWidget(widget);
        DOM.setElementProperty(widget.getElement(), "cellPadding", "1");

        controls.add(textTextBox);
        controls.add(translationTextBox);

        setValue(word);
    }

    @Override
    public void setValue(Word word) {
        if (word != null) {
            wordId = word.getId();

            textTextBox.setValue(word.getText());
            translationTextBox.setValue(word.getTranslation());
        }
    }

    @Override
    public Word getValue() {
        Word word = new Word();

        word.setId(wordId);
        word.setText(textTextBox.getValue());
        word.setTranslation(translationTextBox.getValue());

        return word;
    }

    @Override
    public void setFocus(boolean focused) {
        textTextBox.setFocus(focused);
    }

    @Override
    public HandlerRegistration addCloseHandler(CloseHandler handler) {
        return addHandler(handler, CloseEvent.getType());
    }

    @Override
    public HandlerRegistration addClosedHandler(ClosedHandler handler) {
        return addHandler(handler, ClosedEvent.getType());
    }

    public HandlerRegistration addNavigationHandler(NavigationEvent.Handler handler) {
        return addHandler(handler, NavigationEvent.getType());
    }

    public void setFocus(int fieldIndex, boolean focused) {
        controls.get(fieldIndex).setFocus(focused);
    }

    @UiHandler("deleteButton")
    void onDeleteClick(ClickEvent event) {
        fireEvent(new ClosedEvent());
    }

    @UiHandler({"textTextBox", "translationTextBox"})
    public void onKeyDown(KeyDownEvent event) {
        int code = event.getNativeKeyCode();
        FocusWidget source = (FocusWidget) event.getSource();
        if (code == KeyCodes.KEY_UP) {
            fireEvent(new NavigationEvent(NavigationEvent.Direction.UP, controls.indexOf(source)));
        } else if (code == KeyCodes.KEY_DOWN) {
            fireEvent(new NavigationEvent(NavigationEvent.Direction.DOWN, controls.indexOf(source)));
        }
    }
}
